package top.kindaries.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.kindaries.model.JiraInfo;
import top.kindaries.service.JiraInfoService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * JIRA信息相关接口
 *
 * @ClassName: JiraInfoController
 * @Author: zhangs
 * @Date: Created in 2021-05-18 15:37
 * @Created by IntelliJ IDEA.
 */
@RestController //相当于@Controller和@ResponseBody两个注解
@Api("JIRA信息相关接口")
@RequestMapping("/jiraInfo")
public class JiraInfoController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private JiraInfoService jiraInfoService;

    @ApiOperation("添加JIRA信息")
    @PostMapping("/addJiraInfo")
    public Boolean addJiraInfo(JiraInfo jiraInfo) {
        return jiraInfoService.addJiraInfo(jiraInfo);
    }

    @ApiOperation("删除JIRA信息")
    @GetMapping("/removeJiraInfo")
    public Boolean removeJiraInfo(String jiraId) {
        return jiraInfoService.removeJiraInfo(jiraId);
    }

    @ApiOperation("编辑JIRA信息")
    @PostMapping("/editJiraInfo")
    public boolean editJiraInfo(JiraInfo jiraInfo) {
        return jiraInfoService.editJiraInfo(jiraInfo);
    }

    @ApiOperation("获取JIRA信息列表")
    @PostMapping("/getJiraInfo")
    public PageInfo<JiraInfo> getJiraInfo(JiraInfo jiraInfo) {
        if (jiraInfo.getPageNum() == null) jiraInfo.setPageNum(1);
        if (jiraInfo.getPageSize() == null) jiraInfo.setPageSize(10);
        if (StringUtils.isBlank(jiraInfo.getOrderBy())) {
            jiraInfo.setOrderBy("delivery_date desc");     //默认按照交付日期排序
        }
        return jiraInfoService.getJiraInfo(jiraInfo);
    }

    @ApiOperation("获取JIRA统计信息")
    @PostMapping("/getJiraStats")
    public Map getJiraStats(JiraInfo jiraInfo) {
        jiraInfo.setPageNum(1);
        jiraInfo.setPageSize(0);
        Map<String, Object> rtnMap = new HashMap<>();
        PageInfo<JiraInfo> pageInfo = jiraInfoService.getJiraInfo(jiraInfo);
        List<JiraInfo> jiraInfoList = pageInfo.getList().stream().sorted(
                Comparator.comparing(JiraInfo::getDeliveryDate).reversed()).collect(Collectors.toList());
        int plannedWorkloadTotal = jiraInfoList.stream().mapToInt(JiraInfo::getPlannedWorkload).sum();
        int actualWorkloadTotal = jiraInfoList.stream().mapToInt(JiraInfo::getActualWorkload).sum();
        rtnMap.put("jiraTotal", pageInfo.getTotal()); //需求数
        rtnMap.put("workloadTotal", actualWorkloadTotal); //总工时
        int efficiency = actualWorkloadTotal == 0 ? 100 : plannedWorkloadTotal * 100 / actualWorkloadTotal;
        rtnMap.put("efficiency", efficiency); //完成效率

        Map<String, Long> jiraStateCollect = jiraInfoList.stream().collect(
                Collectors.groupingBy(JiraInfo::getJiraState, Collectors.counting()));
        Map<String, Long> resultsCollect = jiraInfoList.stream().collect(
                Collectors.groupingBy(JiraInfo::getResults, Collectors.counting()));
        Map<Integer, List<JiraInfo>> monthCollect = jiraInfoList.stream()
                .collect(Collectors.groupingBy(JiraInfo::getMouth));
        Map<Integer, Integer> monthTotal = new HashMap<>();
        Map<Integer, Integer> monthEfficiency = new HashMap<>();
        monthCollect.forEach((k, v) -> {
            monthTotal.put(k, v.size());
            int plannedWorkloadSum = 0; //当月计划工作量总和
            int actualWorkloadSum = 0;  //当月实际工作量总和
            for (JiraInfo res : v) {
                plannedWorkloadSum += res.getPlannedWorkload();
                actualWorkloadSum += res.getActualWorkload();
            }
            monthEfficiency.put(k, plannedWorkloadSum * 100 / actualWorkloadSum);
        });
        String[] states = {"已完成", "已交付", "工作中", "设计中", "评估中"};
        List<Map<String, Object>> stateData = new ArrayList<>();
        for (String state : states) {
            Map<String, Object> stateSum = new HashMap<>();
            stateSum.put("name", state);
            stateSum.put("value", jiraStateCollect.get(state));
            stateData.add(stateSum);
        }
        String[] results = {"任务完成", "问题修复", "产品BUG", "使用问题"};
        List<Map<String, Object>> resultsData = new ArrayList<>();
        for (String result : results) {
            Map<String, Object> resultSum = new HashMap<>();
            resultSum.put("name", result);
            resultSum.put("value", resultsCollect.get(result));
            resultsData.add(resultSum);
        }
        Integer[] monthTotalData = new Integer[12];
        Integer[] monthEffData = new Integer[12];
        for (int i = 0; i < 12; i++) {
            monthTotalData[i] = monthTotal.getOrDefault(i, 0);
            monthEffData[i] = monthEfficiency.getOrDefault(i, 100); //默认完成效率为100%
        }
        rtnMap.put("stateData", stateData); //按状态分组统计
        rtnMap.put("resultsData", resultsData); //按结果分组统计
        rtnMap.put("monthTotalData", monthTotalData); //按月份分组统计需求数
        rtnMap.put("monthEffData", monthEffData); //按月份分组统计月效率
        return rtnMap;
    }

}
